<?php
	/**
	 * @author Matthias Eigner | 0625203
	 */
    class User extends Service implements IService  {
        private $user;
        
        function __construct(){
            parent::__construct();
            $this->user = array();
            $this->loadConfig($GLOBALS['config']['root'].'_classes/User/User.config.php'); 
            include($this->config['loc_file']);
        }
        /**
         *  
         */
        public function view($args) {
            /*$action=(isset($args['action'])) ? $args['action'] : '';
            if($action == 'login_form'){
                $target = isset($args['target']) ? $args['target'] : '';
                $render = new ViewDescriptor($this->config['tpl']['login_form']);
                $render->addValue('target', $target);
                return $render->render();
            } else if($action == 'user_actions'){
                if(isset($_SESSION['User'])){
                    $tmp = new ViewDescriptor($this->config['tpl']['ua_loggedin']);
                    return $tmp->render();
                } else {
                    $tmp = new ViewDescriptor($this->config['tpl']['ua_not_loggedin']);
                    return $tmp->render();
                }
            }
            return '';
            */
        	//TODO: User system 
        }
        public function admin($args){
            return '';
        }
        public function run($args){
            if(!isset($args['action'])) return false;
            $action = $args['action'];
            //Login
            if($action == 'login' && isset($args['nick']) && isset($args['pwd'])) {
               return $this->login($args);
            }
            //Logout
            if($action == 'logout' && isset($_SESSION['User'])) {
               return $this->logout();
            }
        }
        
        /**
         * (non-PHPdoc)
         * @see _core/IService::data()
         * @return UserInfo
         */
        public function data($args){
            $id = (isset($args['id'])) ? $args['id'] : '';
            if($id != ''){
	            $this->getUserById($id);
            } else return null;
            
        }  
        
        public function login($args){
        	if(isset($args['nick']) && isset($args['pwd'])) {
                $u = $GLOBALS['Db-I']->login($args['nick'], $args['pwd']);
                if($u !== false){
                    $array = $GLOBALS['Db-I']->getUserById($u, true);//$this->db->data(array('query'=>'SELECT * FROM '.$GLOBALS['db']['db_prefix'].'user WHERE nick=\''.mysql_real_escape_string($args['nick']).'\' AND pwd=\''.md5(mysql_real_escape_string($args['pwd'])).'\';'));
                    $this->user[$array['u_id']] = new UserInfo($array['nick'], $array['u_id'], $array['email'], $array['group']);
                    $_SESSION['User']['id'] = $this->user[$array['u_id']]->getID();
                    $_SESSION['User']['nick'] = $this->user[$array['u_id']]->getNick();
                    $_SESSION['User']['group'] = $this->user[$array['u_id']]->getGroup();
                    $_SESSION['ajax_sID'] = md5(microtime(true));
                    return true;
                } else return false;
            }
        }
        
        public function logout(){
            if(isset($_SESSION['User'])) {
            	$GLOBALS['Db-I']->logout(); // tell Server
        		$this->user[$_SESSION['User']['id']] = null;
                $_SESSION = array();
                session_unset ();
                session_destroy ();
                return true;
            } else return true;
        }
        
        public function getUserById($id){
        	if($id != ''){
        		if(isset($this->user[$id])) return $this->user[$id];
	            $array = $GLOBALS['Db-I']->getUserById($id, true);
	            if(!isset($array['Error'])){
	            	$this->user[$id] = new UserInfo($array['nick'], $array['u_id'], $array['email'], $array['group']);
	            	return $this->user[$id];
	            } else return null;
        	} else return null;
        }
    }
    
    class UserInfo {
        private $nick;
        private $email;
        private $group;
        private $id;
        public function __construct($nick, $id, $email, $group) {
            $this->nick = $nick;
            $this->email = $email;
            $this->id = $id;
            $this->group = $group;
        }
        public function getNick(){ return $this->nick;}
        public function getEmail(){ return $this->email;}
        public function getId(){ return $this->id;}
        public function getGroup() {return $this->group;}
    }
?>